// comboshow.js
var range, token = '';
var sceneres = {};
var app = getApp()
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buycode: 1,
    goodsinfo: {},
    detailpage: true,
    selectattrname: '',
    selectattrprice: '',
    selectattrimg: '',
    toast_flag: false,
    dialogflag: true,
    jumpFlag: false,
    drawerscroll: '105%',
    maskVisual: 'hidden',
    defaults: 'https://img-oss.shihanbainian.com/shihan2/index/defaults.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    var that = this
    sceneres["reg_partner"] = 1
    sceneres["stage"] = 1
    config.goback = 1
    if (options.scene) {
      var scene = decodeURIComponent(options.scene).split('&'); //id=&pid=1694
      if (scene && scene.length > 0) {
        for (var i = 0; i < scene.length; i++) {
          var str = scene[i].split('=')
          if (str[0] == 'pid') {
            this.setData({
              partnerid: str[1]
            })
            sceneres['partner_id'] = str[1]
          } else if (str[0] == 'cid') {
            sceneres['cid'] = str[1]
          }
        }
      }
    } else {
      this.setData({
        partnerid: options.id
      })
      sceneres["partner_id"] = options.id
    }
    console.log(sceneres)
    var res = wx.getSystemInfoSync()
    this.setData({
      winWidth: res.windowWidth
    })

    range = 794 / (750 / this.data.winWidth)
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-in-out',
    });
    this.animation = animation;
    this.animation.translateY(range).step();
    this.setData({
      animationData: this.animation.export()
    });

    app.wechatSwitch().then(function(wechat_switch) { //是否打开微信授权开关
      app.commonLogin(sceneres, wechat_switch)
    })
  },
  // 滚动计算滚动条距离
  onScroll: function (ev) {
    var that = this, scrollTop = ev.detail.scrollTop;
    this.setData({
      backhomeheight: scrollTop > 200 ? 226 : 118,
      backToTop: scrollTop > 200 ? true : false,
      tab_show: scrollTop > 0 ? true : false
    })
  },
  // 返回顶部
  onBackToTop: function () {
    this.setData({
      backToTop: false,
      scrollTop: 0,
      backhomeheight: 118
    })
  },
  backhome: function () {
    wx.switchTab({
      url: '/pages/home/home'
    });
  },
  loadgoodsdetail: function() {
    var that = this
    shihan.sendRequest(config.partnerproduct, 'POST', {
        partner_id: that.data.partnerid
      }, 1)
      .then(function(response) {
        if (response.data.code == 0) {
          // console.log(response.data);
          for (var i = 0; i < response.data.data.sh_product.length; i++) {
            var productstr = response.data.data.sh_product[i]
            productstr.active = false
            for (var b in productstr) {
              if (b&&b.indexOf("price") >= 0 && productstr[b]) {
                var pricestr = (JSON.stringify(productstr[b])).split('.')
                if (pricestr.length == 1) {
                  productstr[b] = productstr[b] + '.00'
                }
              }
            }
          }
          if (response.data.data.sh_content && response.data.data.sh_content.indexOf('<img') > 0) {
            response.data.data.sh_content = response.data.data.sh_content.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;" ')
          }
          that.setData({
            selectattrname: '',
            selectattrprice: '',
            selectattrimg: '',
            selectattrid: '',
            goodsinfo: response.data.data,
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  setModalStatus: function(e) {
    var that = this, token = wx.getStorageSync('token');
    if (token) {
      if (e.currentTarget.dataset.status == 0) {
        if (e.currentTarget.dataset.operate == 'buy') {
          if (that.data.selectattrname == '') {
            shihan.showToast.call(that, '请选择规格');
          } else {
            if (that.data.productparam != "" && that.data.buycode == 1) {
              that.animation.translateY(range).step();
              that.setData({
                showModalStatus: false,
                animationData: that.animation.export(),
                detailpage: true
              });
            }
            if (that.data.buycode == 0) {
              shihan.showToast.call(that, that.data.buy_code_word);
            }
          }
        } else {
          that.animation.translateY(range).step();
          that.setData({
            showModalStatus: false,
            animationData: that.animation.export(),
            detailpage: true
          });
        }
      } else {
        that.animation.translateY(-range).step();
        that.setData({
          showModalStatus: true,
          drawerscroll: 0,
          animationData: that.animation.export(),
          detailpage: false
        });
      }
    } else {
      app.commonNoLogin()
    }
  },
  selectsku: function(e) {
    var that = this
    var big = e.target.dataset.index
    var bigarr = this.data.goodsinfo.sh_product

    if (bigarr[big].active == false) {
      for (var i = 0; i < bigarr.length; i++) {
        bigarr[i].active = false
      }
      bigarr[big].active = true
      this.setData({
        selectattrname: bigarr[big].sh_name,
        selectattrprice: bigarr[big].sh_show_price,
        selectattrimg: bigarr[big].sh_image,
        selectattrid: bigarr[big].sh_id,
        goodsinfo: this.data.goodsinfo
      })
      shihan.sendRequest(config.partnerproduct_buy, 'POST', {
          partner_id: that.data.partnerid,
          product_id: that.data.selectattrid
        })
        .then(function(response) {
          if (response.data.code == 0) {
            that.setData({
              order_pay_url: '/pages/partner/orderpay/orderpay?goods=' + JSON.stringify(response.data.data)
            });
          } else if (response.data.code == 1101) {
            app.commonNoLogin()
          } else {
            that.setData({
              buycode: 0,
              buy_code_word: response.data.msg
            })
          }
        })
    } else {
      bigarr[big].active = false
      this.setData({
        selectattrname: '',
        selectattrprice: '',
        selectattrimg: '',
        order_pay_url: '',
        goodsinfo: this.data.goodsinfo
      })
    }

    // console.log(this.data.selectattrprice)
  },
  closeerror: function() {
    clearInterval(time);
    var time;
    var that = this
    time = setTimeout(function() {
      that.setData({
        toast_flag: false
      })
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    this.loadgoodsdetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      buycode: 1
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    this.setData({
      buycode: 1
    })
  }
})